package com.batchbrake.queue;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import org.jdesktop.swingx.JXLabel;
import org.jdesktop.swingx.JXPanel;

import com.batchbrake.jobs.JobProc;

public class QueueCellRenderer extends JXPanel implements TableCellRenderer {
	private static ImageIcon icnHeaderLeft = new ImageIcon("images/HeaderLeft.png");
	private static ImageIcon icnHeaderMiddle = new ImageIcon("images/HeaderMiddle.png");
	private static ImageIcon icnHeaderRight = new ImageIcon("images/HeaderRight.png");
	private static ImageIcon icnMovie = new ImageIcon("images/MovieIcon.png");
	private static ImageIcon icnStatus = new ImageIcon("images/status.png");
	private static ImageIcon icnJobDone = new ImageIcon("images/Okay_16x16.png");
	
	private QueueEntity entity = null;
	private JXLabel lblInput = null;
	private JXLabel lblOutput = null;
	public JXLabel lblStatus = null;
	public Box statusBox = null;
	public JProgressBar progress = null;
	private SubtitlesTable tblSubtitles = null;
	private SubtitlesTableModel subtitlesModel = null;
	private boolean selected = false;
	
	private int xHeader = 7;
	private int yHeader = 7;

	private boolean jobDone = false;
	
	public QueueCellRenderer() {
		this.initComponents();
	}
	
	private void initComponents() {
		JXPanel vBox = new JXPanel(new BorderLayout());
		
		lblInput = new JXLabel();
		lblInput.setFont(lblInput.getFont().deriveFont(Font.BOLD));
		
		
		lblOutput = new JXLabel();
		lblOutput.setMaximumSize(new Dimension(Short.MAX_VALUE, 20));
		lblOutput.setPreferredSize(new Dimension(Short.MAX_VALUE, 20));
		lblOutput.setFont(lblOutput.getFont().deriveFont(11.0f));
		lblOutput.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
//		lblOutput.setBorder(BorderFactory.createLineBorder(Color.red));
		lblOutput.setIcon(icnMovie);
		
		subtitlesModel = new SubtitlesTableModel();
		tblSubtitles = new SubtitlesTable(subtitlesModel);
		tblSubtitles.setGridColor(Color.white);
		tblSubtitles.setFont(tblSubtitles.getFont().deriveFont(11.0f));
		tblSubtitles.setBorder(BorderFactory.createEmptyBorder(0, 50, 0, 0));
		tblSubtitles.setOpaque(false);
		
		lblStatus = new JXLabel("Process stopped");
		lblStatus.setMaximumSize(new Dimension(Short.MAX_VALUE, 20));
		lblStatus.setPreferredSize(new Dimension(Short.MAX_VALUE, 20));
		lblStatus.setFont(lblStatus.getFont().deriveFont(11.0f));
		lblStatus.setIcon(icnStatus);
		
		progress = new JProgressBar();
		progress.setPreferredSize(new Dimension(100,12));
		progress.setMaximumSize(new Dimension(100,12));
		progress.setMinimumSize(new Dimension(100,12));
		
		statusBox = new Box(BoxLayout.X_AXIS);
		statusBox.add(Box.createHorizontalStrut(2));
		statusBox.add(lblStatus);
		statusBox.add(progress);
		statusBox.add(Box.createHorizontalStrut(10));
		statusBox.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
		statusBox.setOpaque(false);
		statusBox.setVisible(false);
		
		vBox.add(lblOutput, BorderLayout.NORTH);
		vBox.add(tblSubtitles, BorderLayout.CENTER);
		vBox.add(statusBox, BorderLayout.SOUTH);
		vBox.setOpaque(false);
		
		this.setLayout(new BorderLayout());
		this.add(lblInput, BorderLayout.NORTH);
		this.add(Box.createHorizontalStrut(20), BorderLayout.WEST);
		this.add(vBox, BorderLayout.CENTER);
		
		this.setOpaque(false);
		this.setBorder(BorderFactory.createEmptyBorder(yHeader + 3, xHeader + 3, yHeader + 3, 5));
	}
	
	
	
	@Override
	public void paint(Graphics graphic) {
		Graphics2D g = (Graphics2D)graphic;
		g.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
		
		if( selected ) {
//			g.setColor(Color.black);
//			g.fillRoundRect(1, 1, this.getWidth()-2, this.getHeight()-2, 10, 10);
			g.setColor(Color.decode("#dfe4ea"));
//			Color c = Color.decode("#80bf00");
			g.fillRect(0, 0, this.getWidth(), this.getHeight());
			g.setColor(Color.black);
			
//			g.drawImage(icnVerticalShadowInverted.getImage(), 0, 0, this.getWidth(), icnVerticalShadowInverted.getIconHeight(), null);
//			g.drawImage(icnVerticalShadow.getImage(), 0, this.getHeight() - 1 - icnVerticalShadow.getIconHeight(), this.getWidth(), icnVerticalShadow.getIconHeight(), null);
			
			//			g.setStroke(new BasicStroke(2));
//			g.drawRoundRect(1, 1, this.getWidth()-2, this.getHeight()-4, 10, 10);
		}
		
		g.drawImage(icnHeaderLeft.getImage(), xHeader, yHeader, icnHeaderLeft.getIconWidth(), 
				icnHeaderLeft.getIconHeight(), null);
		g.drawImage(icnHeaderRight.getImage(), this.getWidth() - icnHeaderRight.getIconWidth() - xHeader, 
				yHeader, icnHeaderRight.getIconWidth(),	icnHeaderRight.getIconHeight(), null);
		g.drawImage(icnHeaderMiddle.getImage(), icnHeaderLeft.getIconWidth(), yHeader, 
				this.getWidth() - icnHeaderLeft.getIconWidth() - icnHeaderRight.getIconWidth() - xHeader, 
				icnHeaderMiddle.getIconHeight(), null);
		
		super.paint(g);
		
		if( jobDone ) {
			g.setColor(new Color(0.1f, 0.1f, 0.1f, 0.3f));
			g.fillRect(0, 0, this.getWidth(), this.getHeight());
			g.drawImage(icnJobDone.getImage(), this.getWidth() - icnJobDone.getIconWidth() - 10, 10, icnJobDone.getIconWidth(), icnJobDone.getIconHeight(), null);
		}
	}
	

	public int getRendererHeight() {
		Insets i = this.getBorder().getBorderInsets(this);
		int rowHeight = lblInput.getHeight() + lblOutput.getHeight() + i.top + i.bottom;
		if( entity != null ) {
			if( (entity.getSubtitles() != null) && (entity.getSubtitles().size() != 0) ) {
				rowHeight += entity.getSubtitles().size() * tblSubtitles.getRowHeight();
			}
			
			if( (entity.getProc() != null) && (entity.getProc().isWorking()) ) {
				rowHeight += lblStatus.getHeight();
			}
		}
		return rowHeight;
	}
	
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		
		entity = (QueueEntity)value;
		jobDone = entity.isJobDone();

		selected = isSelected;
		lblInput.setText(entity.getInput().getName());
		lblOutput.setText(entity.getOutput().getName());
		
		if( (entity.getSubtitles() != null) && (entity.getSubtitles().size() != 0) ) {
			subtitlesModel.setEntity(entity);
			tblSubtitles.getColumn(SubtitlesTableModel.COL_ENCODING).setMaxWidth(60);
			tblSubtitles.getColumn(SubtitlesTableModel.COL_LANG).setMaxWidth(60);
			tblSubtitles.repaint();
		}

		JobProc job = entity.getProc();
		if( job != null ) {
			statusBox.setVisible(true);
			progress.setMaximum(10000);
			progress.setValue((int)job.getPercent()*100);
			if( job.getEta() == null ) {
				if( job.getPercent() == 0 ) {
					lblStatus.setText("Starting ...");
				} else {
					lblStatus.setText("Estimating remaining time ...");
				}
			} else {
				lblStatus.setText(job.getEta() + "  --  " + job.getFps());
			}
		} else {
			statusBox.setVisible(false);
		}
		
		
		int preferredRowHeight = getRendererHeight();
		int currentRowHeight = table.getRowHeight(row);
		
		if( (currentRowHeight != 0) && (currentRowHeight != preferredRowHeight) ) {
			table.setRowHeight(row, getRendererHeight());
		}
		
		return this;
	}


	
	
	
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((entity == null) ? 0 : entity.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		QueueCellRenderer other = (QueueCellRenderer) obj;
		if (entity == null) {
			if (other.entity != null)
				return false;
		} else if (!entity.equals(other.entity))
			return false;
		return true;
	}
}
